<template>
  <div class="hot-products-table-container">
    <div class="table-header">
      <h3>热门商品排行</h3>
    </div>
    
    <div class="table-responsive">
      <table class="hot-products-table">
        <thead>
          <tr>
            <th class="rank-column">排名</th>
            <th class="product-column">商品</th>
            <th 
              class="sales-column" 
              @click="sortBy('salesVolume')"
              :class="{ 'active-sort': currentSort === 'salesVolume' }"
            >
              销量
              <span class="sort-icon">
                {{ currentSort === 'salesVolume' ? (sortDirection === 'asc' ? '↑' : '↓') : '↕' }}
              </span>
            </th>
            <th 
              class="amount-column" 
              @click="sortBy('totalAmount')"
              :class="{ 'active-sort': currentSort === 'totalAmount' }"
            >
              销售额
              <span class="sort-icon">
                {{ currentSort === 'totalAmount' ? (sortDirection === 'asc' ? '↑' : '↓') : '↕' }}
              </span>
            </th>
            <th 
              class="commission-column" 
              @click="sortBy('commission')"
              :class="{ 'active-sort': currentSort === 'commission' }"
            >
              佣金
              <span class="sort-icon">
                {{ currentSort === 'commission' ? (sortDirection === 'asc' ? '↑' : '↓') : '↕' }}
              </span>
            </th>
          </tr>
        </thead>
        <tbody>
          <tr v-for="(product, index) in sortedProducts" :key="product.id" class="product-row">
            <td class="rank-column">
              <div class="rank-badge" :class="getRankClass(index)">{{ index + 1 }}</div>
            </td>
            <td class="product-column">
              <div class="product-info">
                <div class="product-image" @click="previewImage(product.productImage)">
                  <img :src="product.productImage" :alt="product.productName" />
                </div>
                <div class="product-name">{{ product.productName }}</div>
              </div>
            </td>
            <td class="sales-column">{{ product.salesVolume }}</td>
            <td class="amount-column">{{ product.totalAmount.toFixed(2) }} USDT</td>
            <td class="commission-column">{{ product.commission.toFixed(2) }} USDT</td>
          </tr>
        </tbody>
      </table>
    </div>

    <!-- 图片预览模态框 -->
    <div v-if="showPreview" class="image-preview-modal" @click="closePreview">
      <div class="image-preview-content">
        <img :src="previewImageUrl" alt="商品预览" />
        <button class="close-preview" @click="closePreview">×</button>
      </div>
    </div>
  </div>
</template>

<script lang="ts">
import { defineComponent, ref, computed, onMounted } from 'vue';
import { generateHotProductsData } from '@/utils/mockData';

interface Product {
  id: string;
  productId: string;
  productName: string;
  productImage: string;
  salesVolume: number;
  totalAmount: number;
  commission: number;
}

export default defineComponent({
  name: 'HotProductsTable',
  
  setup() {
    const products = ref<Product[]>([]);
    const currentSort = ref<string>('totalAmount');
    const sortDirection = ref<'asc' | 'desc'>('desc');
    const showPreview = ref<boolean>(false);
    const previewImageUrl = ref<string>('');

    // 获取数据
    onMounted(() => {
      const topProducts = generateHotProductsData(10);
      products.value = topProducts.map(p => ({
        id: p.id,
        productId: p.productId,
        productName: p.productName,
        productImage: p.productImage,
        salesVolume: Math.floor(Math.random() * 1000) + 1, // 随机销量
        totalAmount: p.originalPrice * (Math.floor(Math.random() * 1000) + 1), // 根据原始价格和随机销量计算销售额
        commission: p.commission,
      }));
    });

    // 排序功能
    const sortBy = (column: string) => {
      if (currentSort.value === column) {
        sortDirection.value = sortDirection.value === 'asc' ? 'desc' : 'asc';
      } else {
        currentSort.value = column;
        sortDirection.value = 'desc'; // 默认降序
      }
    };

    // 排序后的产品列表
    const sortedProducts = computed(() => {
      return [...products.value].sort((a, b) => {
        const modifier = sortDirection.value === 'asc' ? 1 : -1;
        const aValue = a[currentSort.value as keyof Product];
        const bValue = b[currentSort.value as keyof Product];
        
        if (typeof aValue === 'number' && typeof bValue === 'number') {
          return (aValue - bValue) * modifier;
        }
        
        return 0;
      });
    });

    // 获取排名样式
    const getRankClass = (index: number) => {
      if (index === 0) return 'rank-first';
      if (index === 1) return 'rank-second';
      if (index === 2) return 'rank-third';
      return '';
    };

    // 预览图片
    const previewImage = (imageUrl: string) => {
      previewImageUrl.value = imageUrl;
      showPreview.value = true;
    };

    // 关闭预览
    const closePreview = () => {
      showPreview.value = false;
    };

    return {
      products,
      currentSort,
      sortDirection,
      sortedProducts,
      sortBy,
      getRankClass,
      showPreview,
      previewImageUrl,
      previewImage,
      closePreview
    };
  }
});
</script>

<style lang="scss" scoped>
.hot-products-table-container {
  background-color: #fff;
  border-radius: 8px;
  box-shadow: 0 2px 12px rgba(0, 0, 0, 0.08);
  overflow: hidden;
  margin-bottom: 20px;

  .table-header {
    padding: 16px 20px;
    border-bottom: 1px solid #eaeaea;

    h3 {
      margin: 0;
      font-size: 18px;
      font-weight: 600;
      color: #333;
    }
  }

  .table-responsive {
    overflow-x: auto;
  }

  .hot-products-table {
    width: 100%;
    border-collapse: collapse;
    
    th, td {
      padding: 12px 16px;
      text-align: left;
      border-bottom: 1px solid #eaeaea;
    }
    
    th {
      font-weight: 600;
      color: #666;
      background-color: #f9f9f9;
      cursor: pointer;
      transition: background-color 0.2s;
      
      &:hover {
        background-color: #efefef;
      }
      
      &.active-sort {
        color: #1989fa;
      }
      
      .sort-icon {
        margin-left: 4px;
        font-size: 12px;
      }
    }
    
    .product-row {
      transition: background-color 0.2s;
      
      &:hover {
        background-color: #f5f7fa;
      }
      
      &:last-child td {
        border-bottom: none;
      }
    }
    
    .rank-column {
      width: 80px;
      
      .rank-badge {
        display: flex;
        align-items: center;
        justify-content: center;
        width: 28px;
        height: 28px;
        border-radius: 50%;
        background-color: #e0e0e0;
        color: #666;
        font-weight: bold;
        
        &.rank-first {
          background-color: #ffd700;
          color: #fff;
        }
        
        &.rank-second {
          background-color: #c0c0c0;
          color: #fff;
        }
        
        &.rank-third {
          background-color: #cd7f32;
          color: #fff;
        }
      }
    }
    
    .product-column {
      width: 30%;
      
      .product-info {
        display: flex;
        align-items: center;
        
        .product-image {
          width: 50px;
          height: 50px;
          margin-right: 12px;
          border-radius: 4px;
          overflow: hidden;
          cursor: pointer;
          transition: opacity 0.2s;
          
          &:hover {
            opacity: 0.8;
          }
          
          img {
            width: 100%;
            height: 100%;
            object-fit: cover;
          }
        }
        
        .product-name {
          font-size: 14px;
          font-weight: 500;
          line-height: 1.4;
          color: #333;
          overflow: hidden;
          text-overflow: ellipsis;
          display: -webkit-box;
          -webkit-line-clamp: 2;
          -webkit-box-orient: vertical;
        }
      }
    }
    
    .sales-column, .amount-column, .commission-column {
      text-align: right;
      width: 15%;
      font-weight: 500;
    }
    
    .commission-column {
      color: #f56c6c;
    }
  }

  // 图片预览模态框
  .image-preview-modal {
    position: fixed;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    background-color: rgba(0, 0, 0, 0.75);
    display: flex;
    align-items: center;
    justify-content: center;
    z-index: 1000;
    
    .image-preview-content {
      position: relative;
      max-width: 90%;
      max-height: 90%;
      
      img {
        max-width: 100%;
        max-height: 90vh;
        object-fit: contain;
        border-radius: 4px;
      }
      
      .close-preview {
        position: absolute;
        top: -20px;
        right: -20px;
        width: 36px;
        height: 36px;
        border-radius: 50%;
        background-color: #fff;
        border: none;
        color: #333;
        font-size: 20px;
        display: flex;
        align-items: center;
        justify-content: center;
        cursor: pointer;
        
        &:hover {
          background-color: #f2f2f2;
        }
      }
    }
  }
}

// 响应式调整
@media (max-width: 768px) {
  .hot-products-table {
    .rank-column {
      width: 60px;
    }
    
    .product-column {
      width: 40%;
    }
    
    th, td {
      padding: 10px 12px;
    }
    
    .product-info {
      .product-image {
        width: 40px;
        height: 40px;
      }
    }
  }
}
</style> 